package com.cheyun.netsalev3.view.mars.choosecar

import android.graphics.Color
import android.util.Log
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.TitleAdapter
import com.cheyun.netsalev3.bean.CarPinPaParam
import com.cheyun.netsalev3.databinding.MarsChooseCarFragmentBinding
import com.cheyun.netsalev3.utils.BaseFragment
import com.cheyun.netsalev3.utils.FunctionUtils
import com.cheyun.netsalev3.viewmodel.MarsChooseCarViewModel
import com.cheyun.netsalev3.widget.AZSideBarView
import com.cheyun.netsalev3.widget.AZTitleDecoration

class MarsChooseCarFragment : BaseFragment<MarsChooseCarFragmentBinding>() {
    override fun initContentView(): Int {
        return R.layout.mars_choose_car_fragment
    }

    override fun initViewModel(): ViewModel? {
        this?.activity?.apply {
            return ViewModelProviders.of(this).get(MarsChooseCarViewModel::class.java)
        }
        return null
    }

    private lateinit var adapter: TitleAdapter<CarPinPaParam>

    override fun initView() {
        adapter = TitleAdapter(R.layout.brand_item, binding.viewModel)
        binding.recyclerView.adapter = adapter
        //头部属性设置
        var titleview = com.cheyun.netsalev3.widget.AZTitleDecoration.TitleAttributes(this!!.context!!)
        titleview.setItemHeight(29).setTextColor(Color.parseColor("#ff212a3b"))
                .setBackgroundColor(Color.parseColor("#ffffffff"))
                .setTextPadding(32).setTextSize(13).setItemMarginTop(10)
                .setLineColor(Color.parseColor("#ffb8bec9"))
        binding.recyclerView.addItemDecoration(AZTitleDecoration(titleview))
    }

    /**
     * 右侧滑动监听
     */
    private fun initEvent() {
        binding.mBarList.setOnLetterChangeListener(object : AZSideBarView.OnLetterChangeListener {
            override fun onLetterChange(letter: String) {
                val position = adapter.getSortLettersFirstPosition(letter)
                if (position != -1) {
                    if (binding.recyclerView.layoutManager is LinearLayoutManager) {
                        val manager = binding.recyclerView.layoutManager as LinearLayoutManager
                        if (position==0){
                            manager.scrollToPositionWithOffset(position, 0)
                        }else{
                            manager.scrollToPositionWithOffset(position, FunctionUtils.dip2px(context!!,-10f))
                        }

                    } else {
                        binding.recyclerView.layoutManager?.scrollToPosition(position)
                    }
                }
            }
        })
    }

    override fun initData() {
        initEvent()
//        binding.viewModel?.dataList?.observe(this, Observer { list ->
//            adapter.temlist = list
//            adapter.submitList(list)
//        })

    }

    override fun lazyInit() {
        Log.i(TAG,"override lazyinit")
    }

    companion object {
        fun newInstance() = MarsChooseCarFragment()
    }


}
